package org.youroute.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AltaUsuarios implements EntryPoint {
	Label lblOcupado=new Label("Ese identificador de usuario ya esta ocupado");
	private final IServicioAltaUsuariosAsync servicio;
	
	public AltaUsuarios(){
	      servicio = (IServicioAltaUsuariosAsync) GWT.create(IServicioAltaUsuarios.class);

	      ServiceDefTarget target = (ServiceDefTarget) servicio;
	      
	      String moduleRelativeURL = GWT.getModuleBaseURL() + "ServicioAltaUsuarios";
	      target.setServiceEntryPoint(moduleRelativeURL);
	}
	
	
  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    final TextBox txt = new TextBox();
    //Button está en com.google.gwt.user.client.ui
    final Button botonAlta = new Button("Nuevo usuario");
    final Button botonLogin = new Button("Entrar");
    final PasswordTextBox pwd = new PasswordTextBox();
    
    txt.addKeyboardListener(new KeyboardListener() {
     public void onKeyDown(Widget sender, char keyCode, int modifiers) {
		// TODO Auto-generated method stub	
	}

	public void onKeyPress(Widget sender, char keyCode, int modifiers) {
		 comprobarNombre(sender);
		
	}

	public void onKeyUp(Widget sender, char keyCode, int modifiers) {
		 comprobarNombre(sender);
	}
	
	private void comprobarNombre(Widget sender){
		String nombre=((TextBox)sender).getText();
		servicio.comprobarNombre(nombre, new AsyncCallback() {
	        public void onFailure(Throwable caught) {
	          System.err.println("Failure");
	        }

	        public void onSuccess(Object result) {
	          if (((Boolean)result).booleanValue()==true){
	        	  RootPanel.get("tdOcupado").add(lblOcupado);
	          }else{
	        	  RootPanel.get("tdOcupado").remove(lblOcupado);
	          }
	        }
	      });
				
	}
	
	
    });
    
    botonAlta.addClickListener(new ClickListener() {
    	public void onClick(Widget sender){
    		servicio.alta(txt.getText(), pwd.getText(), new AsyncCallback(){
    			    public void onFailure(Throwable caught) {
    		        }

    		        public void onSuccess(Object result) {
    		        }
    		      });
    		}
    });
    
    botonLogin.addClickListener(new ClickListener() {
    	public void onClick(Widget sender){
    		Boolean res = servicio.login(txt.getText(), pwd.getText(), new AsyncCallback(){
			    public void onFailure(Throwable caught) {
		        }

		        public void onSuccess(Object result) {
		        }

    		});
    		System.out.println("el resultado del login es: " + res.booleanValue());
    	}
    });
    
    
    RootPanel.get("spnNombre").add(txt);
    RootPanel.get("botonAlta").add(botonAlta);
    RootPanel.get("botonEntrar").add(botonLogin);
    RootPanel.get("spnPass").add(pwd);
  
  
  
  
  }
  

}